package mashuaiproject.mashuaiproject.controller;
import mashuaiproject.mashuaiproject.dto.CommentListDto;
import mashuaiproject.mashuaiproject.dto.CommentsSonDto;
import mashuaiproject.mashuaiproject.dto.QuesionUserDto;
import mashuaiproject.mashuaiproject.mapper.AttributeMappper;
import mashuaiproject.mashuaiproject.mapper.NotificationMapper;
import mashuaiproject.mashuaiproject.mapper.SaveQuesionMapper;
import mashuaiproject.mashuaiproject.model.CommentSon;
import mashuaiproject.mashuaiproject.model.Quesions;
import mashuaiproject.mashuaiproject.model.Save;
import mashuaiproject.mashuaiproject.model.Users;
import mashuaiproject.mashuaiproject.serivce.CommentSerivce;
import mashuaiproject.mashuaiproject.serivce.NotificationSerivce;
import mashuaiproject.mashuaiproject.serivce.QuesionUserSerivce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller//主要对文章的crud
public class QuesionsComtroller {
    @Autowired
    private QuesionUserSerivce quesionUserSerivce;
    @Autowired
    private CommentSerivce commentSerivce;
    @Autowired
    private NotificationMapper notificationMapper;
    @Autowired
    private SaveQuesionMapper saveQuesionMapper;
    @Autowired
    private AttributeMappper attributeMappper;

    //从主页面点击文章进入的功能，并且获取一级评论
    @GetMapping("/quesion/{id}")//这里的id是指文章的id主键
    public  String quesions(
            @PathVariable(name = "id")Integer id
            , Model model,HttpServletRequest request
            ){
        //首先跟新阅读数
        quesionUserSerivce.addViewConut(id);
        //查出问题列表,根据文章id查找comment对应的id,并且带去用户的信息
        List<CommentListDto> commentDtos=quesionUserSerivce.ListBuyQusionId(id,1);//将文章id传递过去
       //将当前文章id，传递到QuesionSerivce中进行和users表的信息关联
        QuesionUserDto  quesionUserDtos=quesionUserSerivce.getByidQuesionUser(id);
        //根据这个文章的标签查找出相关问题
        List<Quesions> connectQuesions=quesionUserSerivce.getByConnectQuesions(id);
        //在将当前的点赞信息传递到页面，进行判断
       // List<clickfabulou> clickfabulous=quesionUserSerivce.getById

        //判断下文章是不是被当前用户被收藏
        Users user = (Users) request.getSession().getAttribute("user");
        if(user!=null){
            //显示改用户是否关注这个帖子
            Integer bozhu_id=quesionUserSerivce.selectCreator(id);
            Integer checkGuanzhu=attributeMappper.checkGuanZhu(bozhu_id,user.getId());
            //找出当前博主的粉丝
            Integer fensiCount=attributeMappper.selectFenSiCount(bozhu_id);
            if (checkGuanzhu!=0){
                //已经关注
                model.addAttribute("isGuanzhu","yes");
            }else{
                model.addAttribute("isGuanzhu","no");
            }
            model.addAttribute("fensiCount",fensiCount);
            //显示是否收藏博客
            Save save2=saveQuesionMapper.selectUserId(id,user.getId());
            if(save2!=null){
                //说明已经点赞过
                model.addAttribute("isSave","save");
            }
            model.addAttribute("userIsLogin","isloging");
        }else{
            model.addAttribute("userIsLogin","nologing");
        }



        //首先要判断下是否已经点赞过。(如果save表中当前的id有这个用户，说明已经点赞
        Integer likeCount=saveQuesionMapper.selectLike(id);
        model.addAttribute("likeCount",likeCount);
        model.addAttribute("connectQuesions",connectQuesions);//与这边文章相关的文章
        model.addAttribute("quesion",quesionUserDtos);
        model.addAttribute("commentList",commentDtos);
        return "quesion";
    }

    //ajax获取二级评论
    @ResponseBody
    @GetMapping ("/getCommentSon")
    public Map<String,Object> getCommentSon(@RequestParam("fatherId") Integer fatherId
    , HttpServletRequest request){
        Map<String ,Object> map=new HashMap<>();
       //根据父评论的id获取当前父评论的子评论
        Users users= (Users) request.getSession().getAttribute("user");//这个是不对。这个智能获取当前的用户名，而我们是需要二级评论人的姓名
        //通过父评论的id。获取当前的用户
        //先通过父评论的id，。获取倒所有的评论
        List<CommentSon> commentSonList=quesionUserSerivce.selectAllCommentSonById(fatherId);
        //在查找出对应等等user对象

        if(users==null){
            map.put("code","2003");
        }
        List<CommentsSonDto> commentsSonDtos=quesionUserSerivce.ListBuyIdCommentSon(fatherId,commentSonList);
        map.put("commentsSonDtos",commentsSonDtos);
        return map;
    }

    @GetMapping("/notified/{ids}")
    public  String  notified_quesion( @PathVariable(name = "ids")String ids
            , Model model,HttpServletRequest request){
       int id=Integer.valueOf(ids.split("-")[0]);
        int notifi_id=Integer.valueOf(ids.split("-")[1]);//notifiedactio对应的主键
        int updateStauts=notificationMapper.updateStatus(notifi_id);
        quesionUserSerivce.addViewConut(id);
        //查出问题列表,根据文章id查找comment对应的id,并且带去用户的信息
        List<CommentListDto> commentDtos=quesionUserSerivce.ListBuyQusionId(id,1);//将文章id传递过去
        //将当前文章id，传递到QuesionSerivce中进行和users表的信息关联
        QuesionUserDto  quesionUserDtos=quesionUserSerivce.getByidQuesionUser(id);
        //根据这个文章的标签查找出相关问题
        List<Quesions> connectQuesions=quesionUserSerivce.getByConnectQuesions(id);
        //在将当前的点赞信息传递到页面，进行判断
        // List<clickfabulou> clickfabulous=quesionUserSerivce.getById
        model.addAttribute("connectQuesions",connectQuesions);//与这边文章相关的文章
        model.addAttribute("quesion",quesionUserDtos);
        model.addAttribute("commentList",commentDtos);

        //判断下文章是不是被当前用户被收藏
        Users user = (Users) request.getSession().getAttribute("user");
        if(user!=null){
            Save save2=saveQuesionMapper.selectUserId(id,user.getId());
            if(save2!=null){
                //说明已经点赞过
                model.addAttribute("isSave","save");
            }
            model.addAttribute("userIsLogin","isloging");
        }else{
            model.addAttribute("userIsLogin","nologing");
        }
        Integer likeCount=saveQuesionMapper.selectLike(id);
        model.addAttribute("likeCount",likeCount);
        return "quesion";
    }

}
